//
//  HomeTopView.m
//  QuantumWar
//
//  Created by mysj on 16/7/8.
//  Copyright © 2016年 zeng. All rights reserved.
//

#import "HomeTopView.h"
#import "RankView.h"
#import <Masonry.h>



#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
const int selfH = 58;
const int magr = 2;

@interface HomeTopView () {

    CGFloat progress;
    BOOL    isH;
}

@property (nonatomic, strong) RankView *            rankView;
@property (nonatomic, strong) UIImageView *         progressBg;
@property (nonatomic, strong) UIProgressView *      progressView;
@property (nonatomic, strong) UILabel *             userNameLbl;
@property (nonatomic, strong) UIButton *            rucksackBtn;

//临时
@property (nonatomic, strong) UIImageView *         progressH;
@property (nonatomic, strong) UIImageView *         progressF;

//进度
//@property (nonatomic, assign) CGFloat               progress;
@end

@implementation HomeTopView

#pragma mark - API
- (void)onClickGeocode {
 
    progress += 0.1;
    NSLog(@"%f",progress);
    if (progress == .1 && !isH) {
        self.progressH.alpha = 1;
        self.progressF.alpha = 0;
        
        [UIView animateWithDuration:.1 animations:^{
            self.progressH.alpha = 1;
            self.progressF.alpha = 0;
        }];
        isH = YES;
        progress = 0.0;
        [self.progressView setProgress:progress animated:YES];
        return;
    }
    if (progress >= 1.0f) {
        self.progressF.alpha = 1;
        [self.progressF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@5.3);
        }];
        
        [UIView animateWithDuration:0.5 animations:^{
            [self layoutIfNeeded];
        }];
        progress = 0;
        isH = NO;
        return;
    }
    
    [self.progressView setProgress:progress animated:YES];
}


#pragma mark - 

- (instancetype)init {
    self = [super init];
    if (self) {
        
        [self prepareSubviews];
    }
    return self;
}

- (void)prepareSubviews {
    [self addSubview:self.rankView];    //等级
    [self addSubview:self.rucksackBtn]; //背包
    [self addSubview:self.progressBg];  //进度
    [self.progressBg addSubview:self.progressView];
    [self.progressBg addSubview:self.progressH];
    [self.progressBg addSubview:self.progressF];
    [self addSubview:self.userNameLbl];  //昵称
    
    [self.rankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.width.equalTo(@57);
        make.height.equalTo(@57);
    }];
    
    [self.rucksackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-3);
        make.top.equalTo(self).offset(5);
        make.width.equalTo(@66);
        make.height.equalTo(@31);
    }];
    
    [self.progressBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rankView.mas_right);
        make.top.equalTo(self).offset(7);
        make.right.equalTo(self.rucksackBtn.mas_left);
    }];
    
    [self.progressH mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBg).offset(4.9);
        make.left.equalTo(self.progressBg.mas_left).offset(2.9);
       
    }];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBg).offset(9.4);
        make.left.equalTo(self.progressBg).offset(5.5);
        make.right.equalTo(self.progressBg).offset(-9);
    }];
    
    [self.progressF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressBg).offset(5);
        make.left.equalTo(self.progressView.mas_right);
    }];
    
    
    [self.userNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rankView.mas_right).offset(5);
        make.top.equalTo(self.progressBg.mas_bottom).offset(5);
    }];
}



- (UIImageView *)progressH {

    if (_progressH == nil) {
        _progressH = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"progressView_00"]];
        _progressH.alpha = 0;
    }
    return _progressH;
}
- (UIImageView *)progressF {
    
    if (_progressF == nil) {
        _progressF = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"progressView_02"]];
        _progressF.alpha = 0;
    }
    return _progressF;
}

- (UILabel *)userNameLbl {
    
    if (_userNameLbl == nil) {
        _userNameLbl = [[UILabel alloc] init];
        _userNameLbl.text = @"leeg128F";
        _userNameLbl.textColor = RGBA(123, 225, 242, 1.0);
    }
    return _userNameLbl;
}

- (UIProgressView *)progressView {
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
        _progressView.progressImage = [UIImage imageNamed:@"progressView_01"];
        _progressView.transform = CGAffineTransformMakeScale(1.0f,4.5f);
    }
    return _progressView;
}

- (UIImageView *)progressBg {
    
    if (_progressBg == nil) {
        _progressBg = [[UIImageView alloc] init];
        [_progressBg setImage:[UIImage imageNamed:@"progress_00"]];
        
    }
    return _progressBg;
}

- (UIButton *)rucksackBtn {
    
    if (_rucksackBtn == nil) {
        _rucksackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rucksackBtn setImage:[UIImage imageNamed:@"backpack_0"] forState:UIControlStateNormal];
    }
    return _rucksackBtn;
}

- (RankView *)rankView {
    
    if (_rankView == nil) {
        _rankView = [RankView rank];
        [_rankView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickGeocode)]];
    }
    return _rankView;
}

@end
